﻿/**
将form表单转成json对象
formID:form表单Id
**/
function FormToJson(formID) {
    var unindexed_array = $('#' + formID).serializeArray();
    var indexed_array = {};
    $.map(unindexed_array, function (n, i) {
        indexed_array[n['name']] = n['value'];
    });
    return indexed_array;
}
/**
  信息列表页打开页面方法(加载列表)
   url：目标页地址
   title：页面窗体标题
   width：窗体宽度
   height：窗体高度
   isRefresh:是否刷新父界面数据列表
   注意：列表加载数据方法必须是loadData
   **/
function InfoOpenPage(url, title, width, height, isRefresh) {
    layer.open({
        type: 2,
        title: title,
        area: [width, height],
        closeBtn: 1,
        maxmin: false, //开启最大化最小化按钮
        content: url,
        //关闭弹出框的回调函数，不管弹出框是手动关闭还是通过脚本进行关闭，都执行
        end: function () {
            if (isRefresh) {
                loadData();
            }
          
        }
    });
}
/**
新页面加载方法(加载当前页码数据列表)
webUrl：目标页面地址
title：打开窗体标题
width:宽度
height:高度
tableId：LayUI数据表ID
curr：当前数据表页码
dUrl:数据源地址
**/
function NewOpenPage(webUrl, title, width, height, tableId, curr, dUrl) {
    var table = layui.table; //LayUI数据表对象
    layer.open({
        type: 2,
        maxmin: false,
        fix: false,
        title: title,
        area: [width, height],
        closeBtn: 1,
        shadeClose: true,
        content: webUrl,
        //关闭弹出框的回调函数，不管弹出框是手动关闭还是通过脚本进行关闭，都执行
        end: function () {
            if (tableId != null && tableId != "") {
                table.reload(tableId, {
                    url: dUrl
                    , page: {
                        curr: curr,//currPage是全局变量，后面会给出它在哪里定义以及赋值
                    }
                });
            }

        }
    });
}
/**
弹出页面关闭方法
**/
function closeWin() {
    var index = parent.layer.getFrameIndex(window.name);
    //关闭当前frame
    parent.layer.close(index);
}
/** 
div高度自适应屏幕
**/
function autoHeight(divId) {
    var height = $(window).height();
    $("#" + divId).height(height - 10);
}
/**
checkbox设置默认选中值
**/
function SetCheckBoxValue(kjName, values) {
    var str = values.split(',');
    $("input[name='" + kjName + "']").each(function () {
        for (var i = 0; i < str.length; i++) {
            if ($(this).attr('value').toLowerCase() == str[i].toString().toLowerCase()) {
                $(this).attr("checked", 'true');
                break;
            }
        }
    });
}
/**
获取Url地址中的参数
**/
function getUrlVariable(variable) {
    var query = decodeURIComponent(window.location.search.substring(1));
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) { return pair[1]; }
    }
    return (false);
}
/**
页面按钮显示设置(暂时注释不用了)
menuCode:页面编码

var userPower = "";//人员权限
var webPower = "";//
function WebPowersShow() {
    var mCode = getUrlVariable("menuCode");
    $.ajax({
        url: "../System/GetWebPowers",
        data: { menuCode: mCode },
        dataType: "json",
        type: "POST",
        async: false,//同步
        success: function (rsl) {
            if (rsl.result) {
                userPower = rsl.data["userPower"];
                webPower = rsl.data["webPower"];
                HideButton();
            }

        }
    })
}


function HideButton() {
    var uArr = userPower.split(",");
    var wArr = webPower.split(",");
    for (var i = 0; i < wArr.length; i++) {
        var isHide = true;
        for (var x = 0; x < uArr.length; x++) {
            if (wArr[i] == uArr[x]) {
                isHide = false;
                break;
            }
        }
        if (isHide) {
            $("#btn-" + wArr[i]).hide();
        }
    }
    $("button").each(function () {
        var btnId = $(this).attr("id");
        if (btnId != undefined) {
            var arr = btnId.split("-");
            var hide = true;
            if (arr.length > 1) {
                for (var i = 0; i < wArr.length; i++) {
                    if (wArr[i] == arr[1]) {
                        hide = false;
                        break;
                    }
                }
            }
            if (hide) {
                $("#" + btnId).hide();
            }
        }
    })
}**/
/**
表格权限隐藏设置
**/
function TablePowerHide() {
    //隐藏无关按钮
    $("div .layui-table-body").find("tr").each(function () {
        var a = $(this).find("td");
        if (a != undefined && a != null && a.length > 0) {
            a.each(function () {
                var field = $(this).attr("data-field");
                if (field != undefined && field != null && field == "right") {
                    var div = a.find("div");
                    if (div != undefined && div != null) {
                        div.find("a").each(function () {
                            var btnId = $(this).attr("id");
                            var isHide = true;
                            for (var i = 0; i < frmPowers.length; i++) {
                                if (btnId == frmPowers[i].pCode) {
                                    isHide = false; break;
                                }
                            }
                            if (isHide) {
                                $(".layui-table-cell #" + btnId).css("display", 'none');
                            }
                        });
                    }
                }
            });
        }
    });
}

/**
表格背景颜色
DivId：表格外层ID
RowIndex：行索引
Color：颜色
**/
function TableRowColor(DivId, RowIndex, Color) {
    try {
        var div = document.getElementById(DivId);
        if (div != null) //找到对象了
        {
            var table_main = div.getElementsByClassName("layui-table-main");   //通过class获取table_main
            if (table_main != null && table_main.length > 0) {
                var table = table_main[0].getElementsByClassName('layui-table');   //通过class获取table
                if (table != null && table.length > 0) {
                    var trs = table[0].querySelectorAll("tr");
                    if (trs != null && trs.length > 0) {
                        trs[RowIndex].style.backgroundColor = Color;//背景颜色
                    }
                }
            }

        }
    }
    catch (e) {
        console.log(e.message);
    }
}
/**
表格背景颜色
DivId：表格外层ID
RowIndex：行索引
tdIndex:单元格索引
Color：颜色
**/
function TableTdColor(DivId, RowIndex, tdIndex, Color) {
    try {
        var div = document.getElementById(DivId);

        if (div != null) //找到对象了
        {
            var table_main = div.getElementsByClassName("layui-table-main");   //通过class获取table_main
            if (table_main != null && table_main.length > 0) {
                var table = table_main[0].getElementsByClassName('layui-table');   //通过class获取table
                if (table != null && table.length > 0) {
                    var trs = table[0].querySelectorAll("tr");
                    let tds = trs[RowIndex].querySelectorAll("td");
                    tds[tdIndex].style.backgroundColor = Color;

                }
            }

        }
    }
    catch (e) {
        console.log(e.message);
    }
}

/*
 * 渲染表单 
 * @param {any} datas：表单数据
 * @param {any} divId：层id
 */
function InitDataForm(datas, divId) {
    $('#' + divId).empty();
    var contents = "";
    for (var i = 0; i < datas.length; i++) {
        let kjStr = "";
        var clist = datas[i].gfields
        var cContents = "";
        var cdiv = "layui-input-inline";
        if (datas[i].singleRow == "1") {
            cdiv = "layui-input-block";//
        }
        for (var x = 0; x < clist.length; x++) {
            if (clist[x].fieldType == "ftext") {
                kjStr = "<input type='text' name='" + clist[x].fieldCode + "' class='layui-input'" +
                    "id='" + clist[x].fieldCode + "'>";
            } else if (clist[x].fieldType == "fxlk") {
                var optionStr = "<option value=''>选择数据</option>";
                if (clist[x].dSoure != "") {
                    var arr = clist[x].dSoure.split(";");
                    for (var y = 0; y < arr.length; y++) {
                        if (arr[y] != "") {
                            var info = arr[y].split(",");
                            optionStr += "<option value='" + info[0] + "'>" + info[1] + "</option>";
                        }
                    }
                }
                kjStr = "<select id='" + clist[x].fieldCode + "' name='" + clist[x].fieldCode + "'>" +
                    optionStr + "</select>";
            }
            cContents += "<label class='layui-form-label'>" + clist[x].fieldName + "</label>" +
                "<div class='" + cdiv + "'>" + kjStr + "</div>";
        }
        contents += "<div class='layui-form-item'>" + cContents + "</div>";
    }
    $('#' + divId).append(contents);
    layui.use('form', function () {
        var form = layui.form;
        form.render();
    });
}
var gjQuery = false;
/**
  时间戳获取方法
  **/
var dateTimeStr = function () {
    var date = new Date();
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    var hour = date.getHours().toString();
    var minutes = date.getMinutes().toString();
    var seconds = date.getSeconds().toString();
    if (hour < 10) {
        hour = "0" + hour;
    }
    if (minutes < 10) {
        minutes = "0" + minutes;
    }
    if (seconds < 10) {
        seconds = "0" + seconds;
    }
    return y + (m < 10 ? ('0' + m) : m) + (d < 10 ? ('0' + d) : d) + hour + minutes + seconds;
}

/**
获取日期
**/
function getDate(n) {
    var date = new Date();
    var year, month, day;
    date.setDate(date.getDate() + n);
    year = date.getFullYear();
    month = date.getMonth() + 1;
    day = date.getDate();
    s = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (day < 10 ? ('0' + day) : day);
    return s;
}
/**
 * 清空form表单
 **/
function clearForm(formId) {
    $('#' + formId)[0].reset();
}
var frmPowers = undefined;
/**
 * 动态加载页面按钮数据
 * webCode:页面编码
 * divId:容器id
 * **/
function BannerData(webCode,divId,othStr) {
    $.ajax({
        url: "../System/GetUserWebOperate",
        data: { menuCode: webCode },
        type: "post",
        dataType: "json",
        success: function (rsl) {
            if (rsl.result) {
                frmPowers = rsl.data["frmPower"];
                loadBanner(rsl.data["btnPower"], divId,othStr);
            }
        }
    });
}
/**
 * 加载页面按钮
 *datas:数据内容
 * divId:容器id
 **/
function loadBanner(datas, divId,othStr) {
    $('#' + divId).empty();
    let liStr = "";
    for (var i = 0; i < datas.length; i++) {
        let jl = "3%";
        if (i == 0) {
            jl = "1%";
        }
        liStr += '<li class="layui-nav-item" style="float:left;margin-left:' + jl + '"><a style="cursor:pointer;">' +
            '<button id="' + datas[i].pCode + '" type="button" name="' + datas[i].pCode + '" class="buttonCss"';
        if (datas[i].wpEvent != "") {
            liStr += ' onclick="' + datas[i].wpEvent + '"';
        }
        let iconStr = "";1005;
        if (datas[i].wpIcon != undefined && datas[i].wpIcon != "") {
            iconStr = '<i class="layui-icon" style="margin-right:3px">&#' + datas[i].wpIcon +';</i>';
        }
        liStr += '>' + iconStr + datas[i].wpName + '</button></a></li>';
    }
    if (othStr != undefined && othStr != "") {
        liStr += othStr;
    }
    $('#' + divId).append('<ul lay-bar="disabled" class="mWebBanner">' + liStr + "</ul>");
}

/**
重载数据表格
tableId:数据表id
dataUrl：数据源地址
currPage：页码
**/
function reLoadTable(tableId, dataUrl, currPage) {
    var table = layui.table; //LayUI数据表对象
    table.reload(tableId, {
        url: dataUrl
        , page: {
            curr: currPage,//currPage是全局变量，后面会给出它在哪里定义以及赋值
        }
    });
}
var tCloumns = "";
/**
获取表格列信息
tCode:表格编码
**/
function getTableCloums(tCode) {
    $.ajax({
        url: "../System/GetTableCloums",
        data: { "tCode": tCode },
        dataType: "json",
        type: "POST",
        async: false,
        success: function (rsl) {
            if (rsl.result) {
                tCloumns = rsl.data;
            } else {
                layer.msg(rsl.message); return;
            }
        }
    });
}